package com.daidai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.daidai.dao.ActivityDao;
import com.daidai.model.Activity;
import com.daidai.model.ActivityDTO;
import com.daidai.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Author: Mr.Chen
 * Date: 2022/08/01 17:44
 * Description:
 */
@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityDao activityDao;

    @Override
    public Activity findActivityById(int activityId) {
        return activityDao.selectById(activityId);
    }

    @Override
    public ActivityDTO findActivityAndNameById(int activityId) {
        return activityDao.findActivityAndNameById(activityId);
    }

    @Override
    public List<Activity> findAllActivity() {
        return activityDao.selectList(null);
    }

    @Override
    public List<Activity> findActivityByAssociationId(int associationId) {
        return activityDao.selectList(new LambdaQueryWrapper<Activity>().eq(Activity::getAssociationId, associationId));
    }

    @Override
    public List<Activity> findActivityByAssociationType(int associationType) {
        return activityDao.findActivityByAssociationType(associationType);
    }

    @Override
    public List<ActivityDTO> findActivityAndNameByAssociationType(int associationType) {
        return activityDao.findActivityAndNameByAssociationType(associationType);
    }

    @Override
    public List<ActivityDTO> findAllActivityAndName() {
        return activityDao.findAllActivityAndName();
    }

    @Override
    public List<Activity> findLastActivity() {
        return activityDao.selectPage(new Page<Activity>(1, 5), new LambdaQueryWrapper<Activity>().orderByDesc(Activity::getStartTime)).getRecords();
    }

    @Override
    public List<ActivityDTO> findAllActivityApply() {
        return activityDao.findAllActivityApply();
    }

    @Override
    public void addActivity(String title, String description, int associationId, String startTime, String endTime, String location, float material, String picture) {
        Activity activity = new Activity();
        activity.setTitle(title)
                .setDescription(description)
                .setActivityStatus(0)
                .setAssociationId(associationId)
                .setStartTime(startTime)
                .setEndTime(endTime)
                .setLocation(location)
                .setMaterial(material)
                .setPicture(picture);
        activityDao.insert(activity);
    }

    @Override
    public void deleteActivity(int activityId) {
        activityDao.deleteById(activityId);
    }

    @Override
    public void updateActivityStatus(int activityStatus, int activityId) {
        activityDao.update(new Activity().setActivityStatus(activityStatus), new LambdaQueryWrapper<Activity>().eq(Activity::getActivityId, activityId));
    }

    @Override
    public void updateActivity(String title, String description,
                               int associationId, String startTime,
                               String endTime, String location,
                               String picture, float material,
                               int activityStatus, int activityId) {
        Activity activity = new Activity();
        activity.setTitle(title)
                .setDescription(description)
                .setActivityStatus(activityStatus)
                .setAssociationId(associationId)
                .setStartTime(startTime)
                .setEndTime(endTime)
                .setLocation(location)
                .setMaterial(material)
                .setPicture(picture)
                .setActivityId(activityId);
        activityDao.updateById(activity);
    }

    @Override
    public void applyPass(int activityId) {
        activityDao.updateById(new Activity().setActivityId(activityId).setActivityStatus(1));
    }

    @Override
    public void applyRefuse(int activityId) {
        activityDao.updateById(new Activity().setActivityId(activityId).setActivityStatus(2));
    }
}
